﻿<%@ Page Language="C#" MasterPageFile="~/Site.master" CodeBehind="Insert.aspx.cs" Inherits="salarioprofessoresInsert.Insert" %>


<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:DynamicDataManager ID="DynamicDataManager1" runat="server" AutoLoadForeignKeys="true" />
    
    <div class="tamanho">

    <h2>Cadastrar Novo Valor</h2>
    
    
    <% 
        
        string tipoErro = Request["erro"].ToString();

        if (tipoErro == "jaExiste")
        {
            string literalcc = "<script language=JavaScript>erro();</script>";
            pnlJS.Controls.Add(new LiteralControl(literalcc));
        }
        if (tipoErro == "dataMenor")
        {
            string literalcc = "<script language=JavaScript>erro2();</script>";
            pnlJS.Controls.Add(new LiteralControl(literalcc));
        }
        
        %>
    
    <asp:ScriptManagerProxy runat="server" ID="ScriptManagerProxy1" />

    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:ValidationSummary ID="ValidationSummary1" runat="server" EnableClientScript="true"
                HeaderText="Erros de validação:" />
            <asp:DynamicValidator runat="server" ID="DetailsViewValidator" ControlToValidate="DetailsView1" Display="None" />

            <asp:DetailsView ID="DetailsView1" runat="server" 
                DataSourceID="DetailsDataSource" DefaultMode="Insert"
                AutoGenerateInsertButton="False" OnItemCommand="DetailsView1_ItemCommand" OnItemInserted="DetailsView1_ItemInserted"
                CssClass="detailstable" FieldHeaderStyle-CssClass="bold" 
                AutoGenerateDeleteButton="False" AutoGenerateRows="False">
                <FieldHeaderStyle CssClass="bold" />
                <Fields>
                
                    <asp:DynamicField DataField="dataInicial" HeaderText="Data inicial " />
                    <asp:DynamicField DataField="dataFinal" HeaderText="Data final " />
                    <asp:DynamicField DataField="valorSalario" HeaderText="Valor " />
                                            
                        
                    <asp:TemplateField ShowHeader="False">
                        <InsertItemTemplate>
                            <%--<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" 
                                CommandName="Insert" Text="Cadastrar" OnClientClick="return validar();"></asp:LinkButton>--%>
                                
                                <asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="True" 
                                CommandName="Insert" Text="Cadastrar" OnClientClick="return validar();" ></asp:LinkButton>
                            &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
                                CommandName="Cancel" Text="Cancelar"></asp:LinkButton>                                
                        </InsertItemTemplate>
                    </asp:TemplateField>
                        
                    
                        
                </Fields>
            </asp:DetailsView>

            <asp:LinqDataSource ID="DetailsDataSource" runat="server" EnableInsert="true">
            </asp:LinqDataSource>
            <Script language="javascript">

                function erro() {
                    alert('Já existe um salário já cadastrado com essas datas.');
                }

                function erro2() {
                    alert('A data final deve ser maior que a data inicial');
                }
                
                function validar() {

                    for (i = 0; i < document.aspnetForm.elements.length; i++) {

                        //localizar o nome do campo

                        var encontrouDesc = document.aspnetForm.elements[i].name.indexOf("dataInicial");

                        if (encontrouDesc >= 0) {

                            //validar


                            if (document.aspnetForm.elements[i].value == "") {

                                alert('A data inicial deve ser informada.');
                                document.aspnetForm.elements[i].focus();
                                return false;

                                // Fim segundo IF
                            }

                            var pegarQuanti = document.aspnetForm.elements[i].value.length;


                            var valor = document.aspnetForm.elements[i].value.toString();
                            var dia = "";
                            var mes = "";
                            var ano = "";
                            var traco = "";


                            for (iz = 0; iz < valor.length; iz++) {

                                if (valor.length == 10) {
                                    if (iz <= 1) {
                                        dia = valor.substr(iz, 1);

                                        if ((dia != "1") && (dia != "2") && (dia != "3") && (dia != "4") &&
                                   (dia != "5") && (dia != "6") && (dia != "7") && (dia != "8") &&
                                   (dia != "9") && (dia != "0")) {

                                            alert('A data inicial deve seguir o seguinte modelo, Ex: dd/mm/aaaa.');
                                            document.aspnetForm.elements[i].focus();
                                            return false;
                                        }
                                    }

                                    if (iz == 2) {
                                        traco = valor.substr(iz, 1);

                                        if (traco != "/") {
                                            alert('A data inicial deve seguir o seguinte modelo, Ex: dd/mm/aaaa.');
                                            document.aspnetForm.elements[i].focus();
                                            return false;
                                        }
                                    }

                                    if ((iz == 3) || (iz == 4)) {

                                        mes = valor.substr(iz, 1);

                                        if ((mes != "0") && (mes != "1") && (mes != "2") &&
                                    (mes != "3") && (mes != "4") && (mes != "5") &&
                                    (mes != "6") && (mes != "7") && (mes != "8") &&
                                    (mes != "9")) {

                                            alert('A data inicial deve seguir o seguinte modelo, Ex: dd/mm/aaaa.');
                                            document.aspnetForm.elements[i].focus();
                                            return false;
                                        }
                                    }

                                    if (iz == 5) {
                                        traco = valor.substr(iz, 1);

                                        if (traco != "/") {
                                            alert('A data inicial deve seguir o seguinte modelo, Ex: dd/mm/aaaa.');
                                            document.aspnetForm.elements[i].focus();
                                            return false;
                                        }
                                    }

                                    if ((iz == 6) || (iz == 7) || (iz == 8) || (iz == 9)) {

                                        ano = valor.substr(iz, 1);

                                        if ((ano != "0") && (ano != "1") && (ano != "2") &&
                                    (ano != "3") && (ano != "4") && (ano != "5") &&
                                    (ano != "6") && (ano != "7") && (ano != "8") &&
                                    (ano != "9")) {

                                            alert('A data inicial deve seguir o seguinte modelo, Ex: dd/mm/aaaa.');
                                            document.aspnetForm.elements[i].focus();
                                            return false;
                                        }
                                    }

                                } else {

                                    alert('A data inicial deve seguir o seguinte modelo, Ex: dd/mm/aaaa.');
                                    document.aspnetForm.elements[i].focus();
                                    return false;
                                }
                            }
                        }
                    }

                    for (i = 0; i < document.aspnetForm.elements.length; i++) {

                        //localizar o nome do campo

                        var encontrouDesc = document.aspnetForm.elements[i].name.indexOf("dataFinal");

                        if (encontrouDesc >= 0) {

                            //validar

                            if (document.aspnetForm.elements[i].value == "") {

                                alert('A data final deve ser informada.');
                                document.aspnetForm.elements[i].focus();
                                return false;

                                // Fim segundo IF
                            }

                            var pegarQuanti = document.aspnetForm.elements[i].value.length;


                            var valor = document.aspnetForm.elements[i].value.toString();
                            var dia = "";
                            var mes = "";
                            var ano = "";
                            var traco = "";

                            for (iz = 0; iz < valor.length; iz++) {

                                if (valor.length == 10) {
                                    if (iz <= 1) {
                                        dia = valor.substr(iz, 1);

                                        if ((dia != "1") && (dia != "2") && (dia != "3") && (dia != "4") &&
                                   (dia != "5") && (dia != "6") && (dia != "7") && (dia != "8") &&
                                   (dia != "9") && (dia != "0")) {

                                            alert('A data final deve seguir o seguinte modelo, Ex: dd/mm/aaaa.');
                                            document.aspnetForm.elements[i].focus();
                                            return false;
                                        }
                                    }

                                    if (iz == 2) {
                                        traco = valor.substr(iz, 1);

                                        if (traco != "/") {
                                            alert('A data final deve seguir o seguinte modelo, Ex: dd/mm/aaaa.');
                                            document.aspnetForm.elements[i].focus();
                                            return false;
                                        }
                                    }

                                    if ((iz == 3) || (iz == 4)) {

                                        mes = valor.substr(iz, 1);

                                        if ((mes != "0") && (mes != "1") && (mes != "2") &&
                                    (mes != "3") && (mes != "4") && (mes != "5") &&
                                    (mes != "6") && (mes != "7") && (mes != "8") &&
                                    (mes != "9")) {

                                            alert('A data final deve seguir o seguinte modelo, Ex: dd/mm/aaaa.');
                                            document.aspnetForm.elements[i].focus();
                                            return false;
                                        }
                                    }

                                    if (iz == 5) {
                                        traco = valor.substr(iz, 1);

                                        if (traco != "/") {
                                            alert('A data final deve seguir o seguinte modelo, Ex: dd/mm/aaaa.');
                                            document.aspnetForm.elements[i].focus();
                                            return false;
                                        }
                                    }

                                    if ((iz == 6) || (iz == 7) || (iz == 8) || (iz == 9)) {

                                        ano = valor.substr(iz, 1);

                                        if ((ano != "0") && (ano != "1") && (ano != "2") &&
                                    (ano != "3") && (ano != "4") && (ano != "5") &&
                                    (ano != "6") && (ano != "7") && (ano != "8") &&
                                    (ano != "9")) {

                                            alert('A data final deve seguir o seguinte modelo, Ex: dd/mm/aaaa.');
                                            document.aspnetForm.elements[i].focus();
                                            return false;
                                        }
                                    }

                                } else {

                                    alert('A data final deve seguir o seguinte modelo, Ex: dd/mm/aaaa.');
                                    document.aspnetForm.elements[i].focus();
                                    return false;
                                }

                            }
                        }
                    }
                    for (i = 0; i < document.aspnetForm.elements.length; i++) {

                        //localizar o nome do campo

                        var encontrouDesc = document.aspnetForm.elements[i].name.indexOf("ValorSalario");

                        if (encontrouDesc >= 0) {

                            //validar


                            if (document.aspnetForm.elements[i].value == "") {

                                alert('O valor do salário deve ser informado.');
                                document.aspnetForm.elements[i].focus();
                                return false;

                                // Fim segundo IF
                            }

                            var valor = document.aspnetForm.elements[i].value.toString();
                            var digito = "";

                            for (iz = 0; iz < valor.length; iz++) {

                                digito = valor.substr(iz, 1);


                                if ((digito != "0") && (digito != "1") && (digito != "2") && (digito != "3") && (digito != "4") && (digito != "5") && (digito != "6") && (digito != "7") && (digito != "8") && (digito != "9") && (digito != ",")) {

                                    alert('O valor do salário deve conter apenas numeros e virgulas.');
                                    document.aspnetForm.elements[i].focus();
                                    return false;

                                }
                            }
                        }
                    }

                    
                    //Fim da função validar  
                }
                // Fim do script
            </script>
<asp:Panel ID="pnlJS" runat="server">
        </asp:Panel>
        </ContentTemplate>
    </asp:UpdatePanel>
    
    </div>
    
</asp:Content>
